<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="false"
      size="small"
    >
      <!-- 状态码列使用不同颜色标识，200使用绿色，4xx使用灰色，5xx使用红色，其他使用黄色 -->
      <template #statusFlag="{ record }">
        <template v-if="record.status == 200">
          <Tag :color="'green'">{{ record.status }}</Tag>
        </template>
        <template v-if="/^4/.test(record.status)">
          <Tag :color="'grey'">{{ record.status }}</Tag>
        </template>
        <template v-if="/^5/.test(record.status)">
          <Tag :color="'red'">{{ record.status }}</Tag>
        </template>
        <template
          v-if="
            record.status != null &&
            record.status != 200 &&
            /^(?!4)/.test(record.status) &&
            /^(?!5)/.test(record.status)
          "
        >
          <Tag :color="'yellow'">{{ record.status }}</Tag>
        </template>
      </template>

      <!-- 耗时列使用不同颜色标识，＞3000使用红色，＞500使用黄色，＜500不需要颜色 -->
      <template #tsFlag="{ record }">
        <template v-if="record.ts < 500 && record.ts != null">
          <Tag>{{ record.ts }}</Tag>
        </template>
        <template v-if="record.ts >= 500 && record.ts < 3000">
          <Tag :color="'yellow'">{{ record.ts }}</Tag>
        </template>
        <template v-if="record.ts >= 3000">
          <Tag :color="'red'">{{ record.ts }}</Tag>
        </template>
      </template>

      <template #toolbar>
        <a-button type="primary" @click="onApiFilterModel"> 日志过滤规则 </a-button>
      </template>
      <template #ip="{ record }">
        <a :href="`https://www.baidu.com/s?wd=${record.ip}`" target="_blank">{{ record.ip }}</a>
      </template>
      <template #token="{ record }">
        <span @click="copyText(record.token)">{{ record.token }}</span>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:eye-line',
              label: '详情',
              onClick: onApilogDetail.bind(null, record),
            },
            {
              icon: 'ant-design:file-text-outlined',
              color: 'warning',
              label: '加入黑名单',
              onClick: onOpenAddBlackList.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <apiLogDetail @register="registerApiLogDetailForm" />
    <apiFiltersTable @register="registerApiFilterModel" @success="afterSuccess" />
    <AddBlackList @register="registerAddBlackListForm" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';

  import { queryApiLog } from '/@/api/platform/log';
  import { apiLogTableColumns, apiLogQueryConditionFormSchema } from './log.data';

  //引入子组件
  import apiLogDetail from './apiLogDetail.vue';
  import apiFiltersTable from './apiFiltersTable.vue';
  import AddBlackList from '../blacklist/AddBlackList.vue';

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';

  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';

  import { useCopyToClipboard } from '/@/hooks/web/useCopyToClipboard';
  const { clipboardRef } = useCopyToClipboard();

  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();

  const [registerApiLogDetailForm, { openModal: openApiLogDetailForm }] = useModal();
  const [registerApiFilterModel, { openModal: onApiFilterTable }] = useModal();
  const [registerAddBlackListForm, { openModal: openAddBlackListForm }] = useModal();

  export default defineComponent({
    name: 'LogManagement', //当前组件名称
    components: { BasicTable, TableAction, apiLogDetail, apiFiltersTable, AddBlackList, Tag },
    setup() {
      //定义模块的名称，提供给子孙组件获取使用
      const moduleName = ref('后台Api日志');

      /**
       * 请求后台查询接口返回分页数据显示
       */
      const [registerTable, { reload }] = useTable({
        title: 'Api日志管理',
        api: queryApiLog, //api中指定的为table加载数据对应的查询方法
        columns: apiLogTableColumns, //表头数据
        formConfig: {
          labelWidth: 100, //设置查询表单中label的宽度
          schemas: apiLogQueryConditionFormSchema, // form查询表单
          showResetButton: false, //查询条件部分中是否显示重置按钮
        },
        pagination: {
          pageSizeOptions: ['10', '20', '50', '100', '200'], //分页组件中每页显示条数的下拉框值
        },
        useSearchForm: true,
        showTableSetting: true, //是否显示表格设置
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 打开Api日志详情窗口
       * @param record 行数据
       */
      function onApilogDetail(record: Recordable) {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateType: OperationTypeEnum.CUSTOME,
          operateString: '查看详情',
          dataId: record.id,
        };
        openApiLogDetailForm(true, propsData);
      }

      /**
       * 打开接口过滤规则弹窗
       */
      function onApiFilterModel() {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateType: OperationTypeEnum.CUSTOME,
          operateString: '接口过滤规则管理',
        };
        onApiFilterTable(true, propsData);
      }

      /**
       * 打开Api日志详情窗口
       * @param record 行数据
       */
      function onOpenAddBlackList(record: Recordable) {
        //使用props传递数据至子组件，在data参数中指定
        let propsData: QcUnifyTransToChildProps = {
          moduleName: moduleName.value,
          operateString: '添加黑名单',
          ip: record.ip,
          url: record.url,
        };
        openAddBlackListForm(true, propsData);
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }

      function copyText(text) {
        clipboardRef.value = text;
        createMessage.success('复制成功');
      }

      return {
        registerTable,
        registerApiLogDetailForm,
        registerApiFilterModel,
        onApilogDetail,
        onApiFilterModel,
        afterSuccess,
        onOpenAddBlackList,
        registerAddBlackListForm,
        copyText,
      };
    },
  });
</script>
